package com.softao.ming.gde.internal.core.model;

import java.io.IOException;
import java.io.InputStream;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.softao.ming.gde.core.model.IBundleGwtPluginFile;
import com.softao.ming.gde.internal.core.util.XmlHelper;

/**
 * 
 */
public class BundleGwtPluginFileImpl implements IBundleGwtPluginFile {
	private boolean mHasGwtClient = false;
	private boolean mGwtClientOnly = false;
	private String mHtmlVersion = HTML_DEFAULT_VERSION;
	
	/**
	 * @param inputStream 
	 * @throws IOException 
	 */
	public BundleGwtPluginFileImpl(final InputStream inputStream) throws IOException {
		Document document = XmlHelper.loadDocument(inputStream);
		Element root = document.getDocumentElement();
		if (!root.getNodeName().equals("gwt.plugin")) {
			throw new RuntimeException("gwt.plugin.xml contains invalid data.");
		}
		
		Element sourceNode = XmlHelper.getOc(root, "source");
		if (sourceNode != null) {
			mHasGwtClient = "true".equalsIgnoreCase(sourceNode.getAttribute("hasGwtClient"));
			mGwtClientOnly = "true".equalsIgnoreCase(sourceNode.getAttribute("gwtClientOnly"));
		}

		String htmlVersionNode = XmlHelper.readOcc(root, "html.version");
		mHtmlVersion = htmlVersionNode == null ? HTML_DEFAULT_VERSION : htmlVersionNode;
	}
	
	@Override
	public String getHtmlVersion() {
		return mHtmlVersion;
	}

	@Override
	public boolean hasClientCode() {
		return mHasGwtClient;
	}

	@Override
	public boolean isClientCodeOnly() {
		return mGwtClientOnly;
	}
	
}
